package com.wangjw.tuling.zookeeper.zkclient;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.Watcher;

import java.util.List;

/**
 * Created by wangjianwen on 2018/12/6.
 */
public class ZkClientWatcher<T> {

    private static final String CONNECT_URL = "47.96.129.248:2181,116.62.115.21:2181,47.96.224.44:2181";

    private ZkClient zkClient;

    public ZkClientWatcher() {
        zkClient = new ZkClient(CONNECT_URL, 5000, 5000, new MyZkSerializer());
    }

    public void createPersistent(String path, Object data) {
        zkClient.createPersistent(path, data);
    }

    public T readData(String path) {
        return zkClient.readData(path);
    }

    public List<String> getChildren(String path) {
        return zkClient.getChildren(path);
    }

    public void writeData(String path, Object object) {
        zkClient.writeData(path, object);
    }

    public void deleteRecursive(String path) {
        zkClient.deleteRecursive(path);
    }

    // 递归创建
    public void createPersistent(String path, boolean createParents) {
        zkClient.createPersistent(path, createParents);
    }

    public void close() {
        zkClient.close();
    }

    public void lister(String path) {
        // 数据监听
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.printf("变更的节点为: %s, %s", dataPath, data);
            }

            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.printf("删除的节点为: %s", dataPath);
            }
        });

        // 子节点监听
        zkClient.subscribeChildChanges(path, new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println("parentPath: " + parentPath + ", currentChilds: " + currentChilds);
            }
        });

        // 状态监听
        zkClient.subscribeStateChanges(new IZkStateListener() {
            @Override
            public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
                if (state == Watcher.Event.KeeperState.SyncConnected) { // 当我重新启动后start，监听触发
                    System.out.println("--------连接成功");
                } else if (state == Watcher.Event.KeeperState.Disconnected) { // 当我在服务端将zk服务stop时，监听触发
                    System.out.println("--------连接断开");
                } else {
                    System.out.println("--------其他状态: " + state);
                }
            }

            @Override
            public void handleNewSession() throws Exception {
                System.out.println("重建session");
            }

            @Override
            public void handleSessionEstablishmentError(Throwable error) throws Exception {

            }
        });
    }

    public static void main(String[] args) throws InterruptedException {
        ZkClientWatcher zkClientWatcher = new ZkClientWatcher();
        String path = "/wangxm";

        /*zkClientWatcher.deleteRecursive(path);
        zkClientWatcher.lister(path);
        zkClientWatcher.createPersistent(path, "Go");
        Thread.sleep(2000);
        zkClientWatcher.writeData(path, "Java");
        Thread.sleep(Integer.MAX_VALUE);*/

        zkClientWatcher.writeData(path, "kid");
    }
}
